package org.serg.match3;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.graphics.drawable.Drawable;

public class Utility {
	
	public static int getScore (int num) {
		if (num == 3) {
			return num;
		}
		return num * (num-2);
	}
	
	public static List<Integer> getNewElements (int num, int img_count) {
		List<Integer> res = new ArrayList<Integer>();
		for (int i=0; i<num; i++) {
			int rand = (int)(Math.random() * img_count);
			res.add(rand);
		}
		return res;
	}
	
	public static List<Jewel> isGroup(Jewel current, Map<Integer, Jewel> jewels, int col_count, int row_count) {
		int cur_type = current.getIndex();
		int cur_position = current.getPosition();
		List<Jewel> result = new ArrayList<Jewel>();
		
		System.out.println("cur_position "+Integer.toString(cur_position));
		
		// get start of row
		// division without remainder
		Integer start_row = cur_position/col_count;
		System.out.println("start_row "+Integer.toString(start_row));
		
		int start_item = start_row*col_count;
		System.out.println("start_item "+Integer.toString(start_item));
		
		List<Jewel> del_row = new ArrayList<Jewel>();
		boolean del_item = false;
		for(int i=start_item; i<start_item+col_count; i++) {
			System.out.println("i "+Integer.toString(i));
			
			Jewel tmp = jewels.get(i);
			if (del_item && cur_type != tmp.getIndex()) {
				break;
			}
			if (!del_item && cur_type != tmp.getIndex()) {
				del_row.clear();
			}
			if (cur_position == tmp.getPosition()) {
				del_item = true;
			}
			if (cur_type == tmp.getIndex()) {
				del_row.add(tmp);
			}
		}
		
		if (del_row.size() > 2) {
			result.addAll(del_row);
			for (Jewel jewel : del_row) {
				System.out.println("delete item "+Integer.toString(jewel.getPosition()));
			}
		}
		
		// get start of col
		int start_col = cur_position-start_item;
		System.out.println("start_col "+Integer.toString(start_col));
		
		List<Jewel> del_col = new ArrayList<Jewel>();
		boolean del_col_item = false;
		for(int i=0; i<row_count; i++) {
			int index = (i * col_count) + start_col;
			System.out.println("delete coll "+Integer.toString(index));
			
			Jewel tmp = jewels.get(index);
			if (del_col_item && cur_type != tmp.getIndex()) {
				break;
			}
			if (!del_col_item && cur_type != tmp.getIndex()) {
				del_col.clear();
			}
			if (cur_position == tmp.getPosition()) {
				del_col_item = true;
			}
			if (cur_type == tmp.getIndex()) {
				del_col.add(tmp);
			}
		}
		
		if (del_col.size() > 2) {
			result.addAll(del_col);
			for (Jewel jewel : del_col) {
				System.out.println("delete item "+Integer.toString(jewel.getPosition()));
			}
		}
		return result;
	}
	
	/*
	 * 0- not Neighbor
	 * 1 - back
	 * 2 - forvard
	 * 3 - down
	 * 4 - up
	 */
	public static int isNeighbor(int current, int previous, int col_count) {
		//back
		if (previous-1 == current && (previous%col_count) != 0) {
				return 1;
			}
		// forvard
		if ((previous+1 == current && (current%col_count) != 0)) {
				return 2;
			}
		// down
		if ((previous+col_count == current)) {
				return 3;
			}
		// up
		if (previous-col_count == current) {
			return 4;
		}
		return 0;
	}
	
	public static void switchElements(Jewel current, Jewel previous) {
		Drawable current_img = current.getImage();
		Drawable previous_img = previous.getImage();
		
		current.sesImage(previous_img);
		previous.sesImage(current_img);
		
		current.draw();
		previous.draw();
		
		int current_index = current.getIndex();
		int previous_index = previous.getIndex();
		
		current.setIndex(previous_index);
		previous.setIndex(current_index);
		
		//current.unblock();
		//previous.unblock();
	}
	
	
}
